package me.sealer.rmi.client;

import me.sealer.rmi.api.DemoService;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Scanner;

/**
 * 这里用一句话描述这个类的作用
 *
 * @author sealer
 * @email 1178884049@qq.com
 * @date 2018年12月07日
 */
public class DemoClient {
    private static final String SERVER_REGISTER_NAME = "Service Rmi Demo";

    public static void main(String[] args) throws NotBoundException, RemoteException {
        int registryPort = 1099;
        Registry registry = LocateRegistry.getRegistry(registryPort);

        DemoService service = (DemoService) registry.lookup(SERVER_REGISTER_NAME);

        Scanner in = new Scanner(System.in);
        String input;
        String result = null;
        while (true) {
            System.out.println("请输入指令， quit 结束服务端程序：\n");
            input = in.next();
            try {
                result = service.echo(input);
            } catch (RemoteException e) {
                if ("quit".equalsIgnoreCase(input)) {
                    System.out.println("服务端程序已结束，无法继续提供服务。");
                } else {
                    throw e;
                }
            }
            System.out.println("从服务端返回的内容： " + result);
            result = null;
        }
    }
}
